package demo1;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/11/17
 * Time: 10:04
 * Description:
 *  RED,GREEN,BLACK; 枚举对象
 *  我们写的枚举类-》默认继承与Java自己的Enum
 */

public enum TestEnum {
    RED("红色",1),GREEN("绿色",3),BLACK("黑色",4);

    private String clo;
    private int ordinal;

    TestEnum(String clo,int ordinal) {
        this.clo = clo;
        this.ordinal = ordinal;
    }

    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("demo1.TestEnum");
            Constructor<?> constructor =
                    c1.getDeclaredConstructor(String.class,int.class,String.class,int.class);
            constructor.setAccessible(true);

            TestEnum testEnum = (TestEnum)constructor.newInstance("棕色",99,"灰色",89);

            System.out.println(testEnum);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
    }

    public static void main2(String[] args) {
        System.out.println(RED.compareTo(BLACK));
        TestEnum[] testEnums = TestEnum.values();
        System.out.println(TestEnum.valueOf("BLACK"));
        /*System.out.println(RED.ordinal());
        for (int i = 0; i < testEnums.length; i++) {
            System.out.println(testEnums[i]+" -> "+testEnums[i].ordinal());//???
        }*/
    }


    public static void main1(String[] args) {
        TestEnum testEnum = BLACK;
        switch (testEnum) {
            case RED:
                System.out.println("red");
                break;
            case BLACK:
                System.out.println("black");
                break;
            case GREEN:
                System.out.println("green");
                break;
            default:
                System.out.println("没有对应的！");
            break;
        }
    }
}
